<?php
declare (strict_types=1);

namespace app\listener\order;


use app\dict\order\OrderDict;
use app\dict\order\OrderLogDict;
use app\service\core\cart\CoreCartService;
use app\service\core\order\CoreOrderLogService;
use app\service\core\order\CoreOrderStatService;
use think\facade\Log;

class AfterShopOrderCreate
{

    public function handle($data)
    {
        Log::write('订单AfterShopOrderCreate' . json_encode($data));

        try {
            $basic = $data['basic'];
            $order_data = $data['order_data'];
            $order_goods_data = $data['order_goods_data'] ?? [];

            //购物车删除
            $cart_ids = $data['cart_ids'] ?? [];
            if ($cart_ids) {
                (new CoreCartService())->deleteByCartIds($cart_ids);
            }

            //发布日志
            $main_type = $data['main_type'] ?? OrderLogDict::MEMBER;
            $main_id = $data['main_id'] ?? $order_data['member_id'];
            (new CoreOrderLogService())->add([
                'order_id' => $order_data['order_id'],
                'status' => OrderDict::WAIT_CHECK,
                'main_type' => $main_type,//todo  可以是传入的
                'main_id' => $main_id,
                'type' => OrderDict::ORDER_CREATE_ACTION,
                'content' => ''
            ]);

            //增加统计数据
            CoreOrderStatService::addStat(['order_num' => 1]);
        } catch ( \Exception $e ) {
            Log::write('订单AfterShopOrderCreate失败' . $e->getMessage() . $e->getFile() . $e->getLine());
        }

    }
}
